/*
 * Created by SharpDevelop.
 * User: Tivadar Papai
 * Date: 11/6/2007
 * Time: 11:19 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using RoverStudio.Common.WorldModel;
using System.Threading;

namespace RoverStudio.MapDisplay.WM2D
{
	/// <summary>
	/// Description of PathMPUC.
	/// </summary>
	public partial class PathMPUC : UserControl, MapPlugin
	{
        private Path currentPath;

        private CaterpillarVehicle rover;

        public CaterpillarVehicle Rover
        {
            get { return rover; }
            set
            {
                if (this.rover != null && this.rover.Controller != null)
                    this.rover.Controller.ControlSignalChanged -= new RoverStudio.Common.Controller.ControlSignalChangedHandler(Controller_ControlSignalChanged);

                rover = value;
                if (currentPath != null)
                    this.currentPath.Rover = rover;
                if (this.rover.Controller != null)
                    this.rover.Controller.ControlSignalChanged += new RoverStudio.Common.Controller.ControlSignalChangedHandler(Controller_ControlSignalChanged);
            }
        }

        private void Controller_ControlSignalChanged(object sender, RoverStudio.Common.Controller.ControlSignalEventArgs csea)
        {
            this.lblRoverControlSignal.Invoke(new ThreadStart( delegate()
            {
                this.lblRoverControlSignal.Text = string.Format("alpha: {0:00.00}, d: {1:00.00}, vl: {2:00.00}, vr: {3:00.00}, t:{4:00.00}",
                    csea.Alpha, csea.Distance, csea.VLeft, csea.VRight, csea.Time);
            }));
        }

 
        
        private WorldModel worldModel;
        private MapDisplay2DControl mapDisplay;

		public PathMPUC()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();

             
            
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}

        #region MapPlugin Members


        public WorldModel WorldModel
        {
            get { return worldModel; }
            set {
                if (currentPath == null)
                {
                    this.currentPath = new Path(value);
                    if (rover != null)
                        this.currentPath.Rover = rover;
                }
                worldModel = value;
            }
        }


        public MapDisplay2DControl MapDisplay
        {
            get { return mapDisplay; }
            set { mapDisplay = value; }
        }

        public void HandleMapInteraction(object sender, MapUserInteractionEventArgs muie)
        {
            switch (muie.type)
            {
                case EventType.MouseBtnLeft:
                    if (currentPath == null)
                        return;
                    currentPath.AddPathNodes(muie.eventPlaceInWorld);
                    break;
                case EventType.MouseBtnRight:
                    if (currentPath == null)
                        return;
                    currentPath.RemoveLast();
                    break;

            }

        }

        public string Description
        {
            get { return string.Format("Path control for: {0}", rover.Name); }
        }
 
        #endregion

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (currentPath == null)
                return;
            currentPath.CreateNewPath();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (rover.Controller != null)
                rover.Controller.StartPathFollowing(currentPath);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (rover.Controller != null)
                rover.Controller.StopPathFollowing();
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (rover.Controller != null)
                rover.Controller.PausePathFollowing();
        }

        private void btnResume_Click(object sender, EventArgs e)
        {
            if (rover.Controller != null)
                rover.Controller.ResumePathFollowing();
        }

        private void btnStep_Click(object sender, EventArgs e)
        {
            if (rover.Controller != null)
                rover.Controller.StepPathFollowing();
        }

        private void cbAutoStep_CheckedChanged(object sender, EventArgs e)
        {
            timerAutoStep.Interval = (int)(numTimerInterval.Value * 1000);
            timerAutoStep.Enabled = cbAutoStep.Checked;
        }

        private void timerAutoStep_Tick(object sender, EventArgs e)
        {
            if (rover.Controller != null)
                rover.Controller.StepPathFollowing();
        }

        private void cbWater_CheckedChanged(object sender, EventArgs e)
        {
            if (cbWater.Checked)    //Set water controller
            {
                rover.Controller = rover.ControllerWater;
            }
            else
            {
                rover.Controller = rover.ControllerGround;
            }
        }
    }
}
